package com.mall.server.service.sms;


import com.mall.dubbo.api.SmsFlashPromotionApi;
import com.mall.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.mall.dubbo.api.SmsFlashPromotionSessionApi;
import com.mall.model.domain.SmsFlashPromotionProductRelation;
import com.mall.model.domain.SmsFlashPromotionSession;
import com.mall.model.vo.SmsFlashPromotionSessionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SmsFlashPromotionSessionService {

    @DubboReference
    private SmsFlashPromotionProductRelationApi smsFlashPromotionProductRelationApi;

    @DubboReference
    private SmsFlashPromotionSessionApi smsFlashPromotionSessionApi;

    @DubboReference
    private SmsFlashPromotionApi smsFlashPromotionApi;

    public List<SmsFlashPromotionSessionVo> selectList(Integer flashPromotionId) {

      /*  SmsFlashPromotion promotion = smsFlashPromotionApi.getById(flashPromotionId);

        List<SmsFlashPromotionSession> list1 = smsFlashPromotionSessionApi.findByCreateTime(promotion.getStartDate(), promotion.getEndDate());
        if (CollUtil.isNotEmpty(list1)) {
            List<Long> ids1 = CollUtil.getFieldValues(list1, "id", Long.class);
            List<SmsFlashPromotionProductRelation> relations = new ArrayList<>();
            for (Long id : ids1) {
                SmsFlashPromotionProductRelation relation = new SmsFlashPromotionProductRelation();
                relation.setFlashPromotionId(promotion.getId());
                relation.setFlashPromotionSessionId(id);
                relations.add(relation);
            }

            smsFlashPromotionProductRelationApi.saveBatch(relations);
        }*/

        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionApi.list();

        List<SmsFlashPromotionProductRelation> counts = smsFlashPromotionSessionApi.productCount(flashPromotionId);

        List<SmsFlashPromotionSessionVo> vos = new ArrayList<>();
        for (SmsFlashPromotionSession smsFlashPromotionSession : list) {
            for (SmsFlashPromotionProductRelation count : counts) {
                if (smsFlashPromotionSession.getId().equals(count.getFlashPromotionSessionId())) {
                    SmsFlashPromotionSessionVo vo = SmsFlashPromotionSessionVo.init(smsFlashPromotionSession, count.getCount());
                    vos.add(vo);
                }
            }
        }

        return vos;
    }

    //获取全部场次
    public List<SmsFlashPromotionSession> list() {

        return smsFlashPromotionSessionApi.list();
    }

    //删除场次
    public void deleteFlash(Integer id) {

        smsFlashPromotionSessionApi.removeById(Long.valueOf(id));
    }

    //添加场次
    public void saveFlash(SmsFlashPromotionSession flashPromotionSession) {

        flashPromotionSession.setCreateTime(new Date());
        smsFlashPromotionSessionApi.save(flashPromotionSession);
    }

    public void updateFlash(Integer id, SmsFlashPromotionSession smsFlashPromotion) {

        smsFlashPromotionSessionApi.updateFlash(id, smsFlashPromotion);

    }

    public SmsFlashPromotionSession findById(Integer id) {

        return smsFlashPromotionSessionApi.getById(Long.valueOf(id));
    }

}
